<?php

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
#
#	FILE:			includes/custom/adminfiles.php
#	FUNCTION:		Custom handler for the Admin Files page
#	AUTHOR:			Cameron Morrow
#	CREATED:		27/07/2005
#
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Check for added file
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
if ($_FILES) {
	
	# Get file name
	$uploaddir = "../assets/uploads/";
	
	$upload_time=0;
	
	Foreach($_FILES["uploadfile"]["name"] as $file_name_value){
		$uploadfile = $uploaddir . basename($file_name_value);
	
		# Move to upload directory
		if (@move_uploaded_file($_FILES["uploadfile"]["tmp_name"][$upload_time], $uploadfile)) {
	
			# Was thumbnail created?
			$thumbnail_created = false;
	
			# Extension
			$extension = substr($_FILES["uploadfile"]["name"][$upload_time], strrpos($_FILES["uploadfile"]["name"][$upload_time], ".") + 1);
	
			# Check if JPG
			if ($extension == "jpg" || $extension == "jpeg") {
	
				# Check if creating thumbnail
				if (@$_POST["createthumbnail"]) {
	
					# Create Thumbnail
					if (createThumbnail($uploaddir, $_FILES["uploadfile"]["name"][$upload_time])) {
						addMessage("<p>Thumbnail was created successfully.</p>");
						$thumbnail_created = true;
					} else {
						addMessage("<p>Sorry, a thumbnail could not be created.</p>", 2);
					}
				}
			}
	
			# Message
			addMessage("<p>OK, file <em>" . $_FILES["uploadfile"]["name"][$upload_time] . "</em> was added to the uploads directory.</p>");
	
			# Log
			addLog(basename($_FILES["uploadfile"]["name"][$upload_time]) . "|" . ($thumbnail_created ? "true" : "false"), 17);
	
		} else {
	
			# Message
			addMessage("<p>Sorry, an error occured while uploading the file. Please try uploading the file again.</p>", 2);
	
		}
		
		$upload_time++;
	}
}

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Check for deleted file
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
if (@$_POST["deletefile"]) {

	# File to delete
	$file_name = $_POST["deletefile"];

	if (deleteFile("../assets/uploads", $file_name)) {

		# Message
		addMessage("<p>OK, file <em>" . $file_name . "</em> was deleted successfully.</p>");

		# Log
		addLog($file_name, 18);

	} else {

		# Message
		addMessage("<p>Sorry, an error occured and file <em>" . $file_name . "</em> could not be deleted.</p>", "error");
	}
}

# Get list of files
$file_search = @$_GET["searchfile"] ? $_GET["searchfile"] : "";
$uploaded_files = getUploadedFiles(true, $file_search);

# Output
addContent("<h2>Search:</h2><form name=\"searchfileform\" action=\"" . $PAGE_ID . ".php\" method=\"get\">" .
	"<p>Search for file: <input type=\"text\" name=\"searchfile\" size=\"50\" value=\"" . $file_search . "\" /> <input type=\"submit\" value=\"Search\" /> <input type=\"button\" onclick=\"window.location = '" . $PAGE_ID . ".php';\" value=\"View All\" /></p>" .
"</form>");

# Prep a table properties object for the files
$data_properties = new TableProperties();

$data_properties -> addCustomColumn("File", "\"<a href=\\\"../assets/uploads/[filename]\\\">[filename][thumbnail]</a>\"");
$data_properties -> addCustomColumn("Size", "formatBytes([size])");
$data_properties -> addCustomColumn("Action", "\"<a href=\\\"javascript:deleteUploadedFile('[filename]');\\\">Delete</a>\"");

# Get files
$current_data = array();
foreach ($uploaded_files as $key => $file_group) {
	foreach ($file_group as $file_details) {
		$current_data[] = array("filename" => $file_details["name"],
			"type" => $FILE_TYPE_NAMES_ARRAY[$key],
			"thumbnail" => ($file_details["thumbnail"] ? "</a> <a href=\\\"../assets/uploads/thumbnails/" . $file_details["name"] . "\\\">(Thumbnail)" : ""),
			"size" => $file_details["size"]
		);
	}
}

# Draw the table
addContent($data_properties -> drawBrowseTable($current_data, "Browsing Files", "type", "upload"));

# Delete file form
addContent("<form name=\"deletefileform\" id=\"deletefileform\" action=\"" . $PAGE_ID . ".php\" method=\"post\">" .
	"<input type=\"hidden\" name=\"deletefile\" id=\"deletefile\" value=\"\" />" .
	"</form>");

# Add file form
addContent("<h2 class=\"uploadadd\">Adding a file</h2>" .
	"<p>Select the file to be added, then click \"Add File\" to upload it:</p>" .
	"<form name=\"addfileform\" id=\"addfileform\" action=\"" . $PAGE_ID . ".php\" enctype=\"multipart/form-data\" method=\"post\">" .
	"<p>How many files do you want to upload at the same time? 
	<select onchange=\"uploadnumber(this.value)\"><option value=\"1\" selected=\"selected\">1</option><option value=\"2\">2</option><option value=\"3\">3</option><option value=\"4\">4</option><option value=\"5\">5</option><option value=\"6\">6</option><option value=\"7\">7</option><option value=\"8\">8</option><option value=\"9\">9</option><option value=\"10\">10</option></select>
	<div id=\"divfile\" style=\"margin-left:80px;\">
		<input type=\"file\" name=\"uploadfile[]\" id=\"uploadfile[]\" width=\"300\"/><br/>
	</div>
	<div style=\"margin-left:80px;\">
	<input type=\"checkbox\" id=\"createthumbnail\" name=\"createthumbnail\" /> 
	<label for=\"createthumbnail\">Create thumbnail (for JPG only)</label> <input type=\"submit\" value=\"Add File\" /></div></p>" .
	"</form>");
	
/*addContent("<div id=\"files_list\"></div><script>var multi_selector = new MultiSelector( document.getElementById( 'files_list' ), 3 );multi_selector.addElement( document.getElementById( 'uploadfile' ) );</script>");*/

# Result form
addContent("<form name=\"searchitemform\" id=\"searchitemform\" action=\"" . $PAGE_ID . ".php\" method=\"post\">" .
	"<input type=\"hidden\" name=\"result\" id=\"result\" value=\"1\" />" .
	"</form>");

?>